﻿using System;
using ParboiledSharp.parboiled.dataobjects;

namespace ParboiledSharp.parboiled.rules
{
    class StringRule : Rule
    {
        private readonly bool _ignoreCase;
        private readonly char[] _match;

        public StringRule(string match, bool ignoreCase)
        {
          _match = ignoreCase ? match.ToUpperInvariant().ToCharArray() : match.ToCharArray();

          _ignoreCase = ignoreCase;
        }

      internal override bool Execute(Context context)
        {
            if (_ignoreCase)
            {
                foreach (char c1 in _match)
                {
                  char c2 = context.GetNextChar();
                  c2 = Char.ToUpperInvariant(c2);
                  if (c1 != c2)
                  {
                    return false;
                  }
                }
            }
            else
            {
                foreach (char c1 in _match)
                {
                  char c2 = context.GetNextChar();
                  if (c1 != c2)
                  {
                    return false;
                  }
                }
            }

            return true;
        }
        
        public override String ToString(){
            return "String rule match[" + new String(_match)+"]";
        }
    }
}